//
//  SLMediatorManager.m
//  Pods
//
//  Created by CoderSLZeng on 2017/11/22.
//  

#import "SLMediatorManager.h"

#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)

#import <objc/runtime.h>

@implementation SLMediatorManager

+ (id)performTarget:(NSString *)targetName action:(NSString *)actionName params:(id)params isRequiredReturnValue: (BOOL)isRequiredReturnValue
{
    
    // 1.获取目标
   Class targetCls = NSClassFromString(targetName);
    if (!targetCls) {
        NSLog(@"目标不存在");
        return nil;
    }
    
    // 2.获取方法
    SEL sel = NSSelectorFromString(actionName);
    if (!sel) {
        NSLog(@"方法不存在");
        return nil;
    }
    
    // 3.判断方法是否存在
    if (![targetCls respondsToSelector:sel]) {
        NSLog(@"目标不存在该方法");
        return nil;
    }
    
    if (isRequiredReturnValue) {
        SuppressPerformSelectorLeakWarning(return [targetCls performSelector:sel withObject:params]);
    } else {
        SuppressPerformSelectorLeakWarning([targetCls performSelector:sel withObject:params]);
    }
    
    return nil;
}
@end
